uni-app App 开发记录
版本号获取
plus.runtime.getProperty(`${plus.runtime.appid}`, (widgetInfo) => {
if (!widgetInfo) {
return;
}
console.log(widgetInfo.version);
});
安卓权限获取校验
const Manifest: any = plus.android.importClass("android.Manifest");
const MainActivity: any = plus.android.runtimeMainActivity();
// WRITE_EXTERNAL_STORAGE 为想要校验的权限,此处是相册保存权限校验
const permissionStatus = MainActivity.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE
);
if (permissionStatus === 0) {
// 权限已允许逻辑
} else {
// 权限未申请或已拒绝逻辑
}
- 参考:App 权限判断和提示
h5 跳转 APP
export default {
data() {
return {
timer: null,
openIng: false,
getSchemesUrl: "跳转Schemes",
};
},
onHide() {
clearTimeout(this.timer);
},
methods: {
getTips() {
// 弹窗提醒用户
uni.showModal({
title: "提醒",
content: "未安装APP? 是否下载?",
confirmText: "去下载",
success: function(res) {
if (res.confirm) {
// 跳转下载页面
console.log("用户点击确定");
} else if (res.cancel) {
console.log("用户点击取消");
}
},
});
},
jumpApp() {
if (this.openIng) {
return;
}
// 判断当前设备是否为苹果设备
const isIOS = uni.getSystemInfoSync().osName === "ios";
if (isIOS) {
// IOS处理
window.location = this.getSchemesUrl;
this.timer = setTimeout(() => {
// 未安装的情况
this.openIng = false;
// 跳转app store
this.getTips();
}, 5000);
} else {
// 安卓处理
const ifr = document.createElement("iframe");
ifr.src = this.getSchemesUrl;
ifr.style.display = "none";
document.body.appendChild(ifr);
this.timer = window.setTimeout(() => {
// 未安装的情况
this.openIng = false;
document.body.removeChild(ifr);
// 提示下载
this.getTips();
}, 5000);
}
},
},
};
应用完整性校验(防止被二开)
// signCheck.js
// #ifdef APP-PLUS
// 开发环境标识
const isDev = ''
// android 应用签名证书SHA-1值,全小写并且中间不包含“:”符号
const appSha1 = ''
// Apple Bunld ID(AppID)的md5值
const appIosMd5 = ''
// #endif
export default () => {
// #ifdef APP-PLUS
if(isDev) {
return
}
// 签名证书检验
let platform = uni.getSystemInfoSync().platform;
let sign = plus.navigator.getSignature();
if ('android' === platform) { //Android平台
if (appSha1 !== sign) {
//证书不对时退出应用
plus.runtime.quit();
}
} else { //iOS平台
if (appIosMd5 !== sign) {
//不进入应用或循环弹出提示框
uni.showModal({
title: '错误',
content: '应用被破坏,无法正常运行!',
showCancel: false
});
}
}
// #endif
}
// App.vue
import signCheck from "@/signCheck"
export default {
onShow: function() {
// #ifdef APP-PLUS
signCheck()
// #endif
}
}
用户是否同意隐私政策
- 用户未同意隐私政策
!plus.runtime.isAgreePrivacy()
- 隐私政策校验
// showPrivacy.js
export default () => {
return new Promise((resolve, reject) => {
// #ifdef APP-PLUS
// 用户已同意隐私政策 或 ios 环境
if(uni.getSystemInfoSync().osName === 'ios' || plus.runtime.isAgreePrivacy()) {
resolve(true)
return
}
const options = {
success: (response) => {
// 用户同意隐私政策
if(response.code === 1) {
// 如果项目中使用了 map、push、Statistic,或者设置loadNativePlugins为false时,用户选择同意隐私政策协议后需要调用plus.runtime.restart重启应用才能生效!
uni.showModal({
title: '提示',
content: '请手动重启应用,并授权相关权限!',
showCancel: false,
confirmText: '重启应用',
success: (res) => {
if (res.confirm) {
plus.runtime.restart();
}
}
})
} else if(response.code === -1) {
// 用户选择退出应用
uni.showModal({
title: '提示',
content: '如需退出应用,请手动退出!',
showCancel: false,
success: (res) => {
// 自定义逻辑
}
});
} else{
//用户未同意隐私政策,游客模式,建议跳转首页
// plus.runtime.restart();
}
},
fail:function(response){
console.log("fail " + JSON.stringify(response));
}
};
//弹出隐私政策协议框,引导用户同意隐私政策
plus.runtime.showPrivacyDialog(options);
reject(false)
// #endif
// #ifndef APP-PLUS
resolve(true)
// #endif
})
}
- 使用
import showPrivacy from '@/showPrivacy.js'
try {
await showPrivacy()
// 后续已同意逻辑
} catch (e) {
// 未同意逻辑
}
...不定时更新
Powered by Waline v2.15.8